package com.situ.stmall.admin.mapper;

import com.situ.stmall.admin.entity.Goods;
import com.situ.stmall.admin.entity.GoodsSort;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface GoodsMapper {
    @Delete({
        "delete from goods",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into goods (goods_sort_id, ",
        "name, dscp, price, ",
        "version, color, ",
        "suit, count)",
        "values (#{goodsSortId,jdbcType=INTEGER}, ",
        "#{name,jdbcType=VARCHAR}, #{dscp,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL}, ",
        "#{version,jdbcType=VARCHAR}, #{color,jdbcType=VARCHAR}, ",
        "#{suit,jdbcType=VARCHAR}, #{count,jdbcType=INTEGER})"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insert(Goods record);

    @Select({
        "select",
        "id, goods_sort_id, name, dscp, price, version, color, suit, count, status",
        "from goods",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_sort_id", property="goodsSortId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
        @Result(column="price", property="price", jdbcType=JdbcType.DECIMAL),
        @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
        @Result(column="color", property="color", jdbcType=JdbcType.VARCHAR),
        @Result(column="suit", property="suit", jdbcType=JdbcType.VARCHAR),
        @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    Goods selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, goods_sort_id, name, dscp, price, version, color, suit, count, status",
        "from goods"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_sort_id", property="goodsSortId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
        @Result(column="price", property="price", jdbcType=JdbcType.DECIMAL),
        @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
        @Result(column="color", property="color", jdbcType=JdbcType.VARCHAR),
        @Result(column="suit", property="suit", jdbcType=JdbcType.VARCHAR),
        @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    List<Goods> selectAll();

    @Update({
    	"<script>",
        "update goods",
        "<set>",
        "<if test = 'goodsSortId != null'> goods_sort_id=#{goodsSortId}, </if>",
        " <if test = 'name != null'> name=#{name}, </if>" ,
        " <if test = 'dscp != null'> dscp=#{dscp}, </if>" ,
        " <if test = 'price != null'> price=#{price}, </if>" ,
        " <if test = 'dscp != null'> dscp=#{dscp}, </if>" ,
        "<if test = 'price != null'> price=#{price}, </if>" ,
        "<if test = 'version != null'> version=#{version}, </if>" ,
        " <if test = 'color != null'> color=#{color}, </if>" ,
        " <if test = 'suit != null'> suit=#{suit}, </if>" ,
        "<if test = 'count != null'> count=#{count}, </if>" ,
        "<if test = 'status != null'> status=#{status}, </if>" ,
        "</set>",
        "where id = #{id,jdbcType=INTEGER}",
        "</script>"
    })
    int updateByPrimaryKey(Goods record);
    
    @Select({
    	"<script>",
        "select * from goods",
        "<where>",
        "<if test='goodsSortId!=null'> goodsSortId = #{goodsSortId} </if>",
        "<if test='name!=null'> name like '%${name}%' </if>",
        "<if test='dscp!=null'> and dscp = #{dscp} </if>",
        "<if test='price!=null'> and price = #{price} </if>",
        "<if test='version!=null'> and version = #{version} </if>",
        "<if test='color!=null'> and color = #{color} </if>",
        "<if test='suit!=null'> and suit = #{suit} </if>",
        "<if test='status!=null'> and status = #{status} </if>",
        "</where>",
        "</script>"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_sort_id", property="goodsSortId", jdbcType=JdbcType.VARCHAR),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
        @Result(column="price", property="price", jdbcType=JdbcType.INTEGER),
        @Result(column="version", property="version", jdbcType=JdbcType.INTEGER),
        @Result(column="color", property="color", jdbcType=JdbcType.INTEGER),
        @Result(column="suit", property="suit", jdbcType=JdbcType.INTEGER),
        @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column = "goods_sort_id", property = "goodsSort", 
        		one = @One(select = "com.situ.stmall.admin.mapper.GoodsSortMapper.selectByPrimaryKey")),
        @Result(column = "id", property = "pictures", 
        		many = @Many(select = "com.situ.stmall.admin.mapper.GoodsPicturreMapper.selectByGoodsId"))	
    })
    List<Goods> select(Goods goods);
}